package com.carnation.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

public class TimeOutFilter implements Filter{
	Logger logger = Logger.getLogger(TimeOutFilter.class);
	@Override
	public void destroy() {
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest httpServletRequest = (HttpServletRequest)request;
		HttpServletResponse httpServletResponse = (HttpServletResponse)response;
		logger.info(httpServletRequest.getServletPath());
		String path = httpServletRequest.getServletPath();
		if(path.toLowerCase().contains("login")){
			chain.doFilter(httpServletRequest, httpServletResponse);
			return ;
		}
		if(httpServletRequest.getSession().getAttribute("sessionInfo") == null){
			logger.info("已过期");
			httpServletRequest.setAttribute("msg", "您还没有登录或登录超时");
			httpServletRequest.getRequestDispatcher("/common/noSession.jsp").forward(httpServletRequest, httpServletResponse);
			return ;
		}else{
			logger.info("未过期");
			chain.doFilter(httpServletRequest, response);
		}
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		
		
	}

}
